package com.dream.technology.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.dream.technology.db.entity.RcFieldType;
import com.dream.technology.db.entity.RcFieldTypeQuery;
import com.dream.technology.data.GenericRspDTO;
import com.dream.technology.service.RcFieldTypeService;
import com.dream.technology.vo.ErrorCode;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/risk/admin/field_type")
public class RcFieldTypeController {

    private static final Logger logger = LoggerFactory.getLogger(RcFieldTypeController.class);

    @Autowired
    private RcFieldTypeService rcFieldTypeService;

    /**
     * 字段分类列表接口
     * @param pageNum
     * @param pageSize
     * @param fieldTypeName
     * @param typeId
     * @return
     */
    @GetMapping("/list")
    public GenericRspDTO<Object> getList(Integer pageNum, Integer pageSize, String fieldTypeName, Integer typeId){

        RcFieldTypeQuery rcFieldTypeQuery=new RcFieldTypeQuery();
        PageHelper.startPage(pageNum,pageSize);
        List<RcFieldType> rcFieldList = rcFieldTypeService.selectByExample(rcFieldTypeQuery);
        PageInfo pageInfo = new PageInfo(rcFieldList);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), pageInfo);
    }

    /**
     *  添加 字段分类
     * @param rcFieldType
     * @return
     */
    @PostMapping("/add")
    public GenericRspDTO<RcFieldType> addFieldType(@RequestBody RcFieldType rcFieldType){

        rcFieldType.setStatus(true);
        rcFieldType.setEnabled(true);
        rcFieldTypeService.insertSelective(rcFieldType);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);

    }

    /**
     * 获取字段分类回显接口
     * @param id
     * @return
     */
    @PostMapping("/id")
    public GenericRspDTO<RcFieldType> getFieldTypeById(Long id){

        RcFieldType rcFieldType = rcFieldTypeService.selectByPrimaryKey(id);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), rcFieldType);
    }

    /**
     * 修改字段接口
     * @param rcFieldType
     * @return
     */
    @PostMapping("/edit")
    public GenericRspDTO<Object> updateFieldType(RcFieldType rcFieldType){

        rcFieldTypeService.updateByPrimaryKeySelective(rcFieldType);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);
    }

    /**
     *  删除字段接口 (逻辑删除)
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public GenericRspDTO<Object> deleteFieldType(Long id){


        RcFieldType fieldType = new RcFieldType();
        fieldType.setStatus(false);
        RcFieldTypeQuery query = new RcFieldTypeQuery();
        RcFieldTypeQuery.Criteria criteria = query.createCriteria();
        criteria.andIdEqualTo(id);
        rcFieldTypeService.updateByExampleSelective(fieldType,query);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);
    }
}
